package identical_scheduling_algorithm;



import java.util.ArrayList;

import de.cau.apo.problems.identicalscheduling.*;

/**
 * Diese Klasse teilt die Jobs in große und kleine Jobs ein und rundet die Großen Jobs
 * 
 * 
 * @author Oliver Hense
 * @author Tim Dopke
 *
 */

public class RoundJobs {
	
	private ArrayList<Double> smallJobs = new ArrayList<Double>();
	
	public ArrayList<Double> getSmallJobs() {
		return smallJobs;
	}
	
	public  double [] roundJobs(double delta, double makeSpan, Job [] jobs){

		//calculate biggest Job
	    int max_value = findMax(jobs);
	    
	    // calculate array size
	    int arraySize = (int)(Math.ceil(Math.log(max_value/(delta* makeSpan)) / Math.log(1+ delta)));
	    if(arraySize < 0){
	    	System.out.println("Es gibt keine großen Jobs fuer dieses Delta!");
	    	return null;
	    }else{
	    	
 	   // Only big Jobs get calculated
	    double [] roundedJobs =new double[arraySize+1];
		for(int i =0; i< jobs.length; i++){
			int pos =(int)(Math.ceil(Math.log(jobs[i].size/(delta* makeSpan)) / Math.log(1+ delta)));
			if (pos >-1){
			roundedJobs[pos]++;
			}
			else{
				double size =  (jobs[i].size/ (makeSpan));
				smallJobs.add(size);
			}
			
		}
		return roundedJobs;
	    }
	
	}
	
	
	private static int findMax(Job[] array) {

	    int max_value = Integer.MIN_VALUE;

	    for (int i =0; i< array.length;i++ ) {
	        if ( array[i].size > max_value ) {
	            max_value = array[i].size;
	        }
	    }
	    return max_value;
	}
	

}
